package com.quanyan.common.utils;

import com.alibaba.fastjson.JSONObject;

/**
 * DistanceUtil
 *
 * @author Tu Chun
 * @date 2016/8/1
 */
public class DistanceUtil {
    //地球平均半径
    private static final double EARTH_RADIUS = 6378137;

    //把经纬度转为度（°）
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为千米
     *
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     * @return
     */
    public static double getDistance(double lng1, double lat1, double lng2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(
                Math.sqrt(
                        Math.pow(Math.sin(a / 2), 2)
                                + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
                )
        );
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s * 1.0 / 1000.0;
    }

    /**
     * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
     *
     * @param lng1
     * @param lat1
     * @param lng2
     * @param lat2
     * @return
     */
    public static double getDistanceRice(double lng1, double lat1, double lng2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(
                Math.sqrt(
                        Math.pow(Math.sin(a / 2), 2)
                                + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
                )
        );
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return Math.floor(s);
    }


    /*{"status":0,"result":{"location":{"lng":115.95845796638,"lat":28.696117043877},
        "formatted_address":"江西省南昌市青山湖区创新路1号","business":"高新开发区,火炬广场,发展路","addressComponent":
        {"city":"南昌市","district":"青山湖区","province":"江西省","street":"创新路","street_number":"1号"},"cityCode":163}}*/

    /**
     * 根据维度经度获取位置信息 百度地图
     *
     * @param lat
     * @param lng
     * @return
     */
    public static String getLocationInfoByLatLng(String lat, String lng) {

        JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result");
        return obj.getString("formatted_address");
    }

    public static JSONObject getLocationInfo(String lat, String lng) {
        String ak = "2vgQnXaL8lsfoFNCZFqH33smsmtR0UcF";
        String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat + ","
                + lng + "&output=json&ak=" + ak + "&pois=0";
        JSONObject obj = JSONObject.parseObject(HttpUtil.getRequest(url));
        return obj;
    }

   /*public static void main(String[] args) {
        System.out.println(getLocationInfoByLatLng("22.545098","113.959423"));
    }*/

 /*public static void main(String[] args){
        double distance = getDistance(121.491909,31.233234,121.411994,31.206184);
        System.out.println("Distance is:"+distance);
    }*/
}
